var path = require('path');
var gulp = require('gulp'),
    gutil = require('gulp-util'),
    minifycss = require('gulp-minify-css'),
    concat = require('gulp-concat'),
    coffee = require('gulp-coffee'),
    coffeelint = require('gulp-coffeelint'),
    sourcemaps = require('gulp-sourcemaps'),
    jshint = require('gulp-jshint'),
    less = require('gulp-less'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    jst = require("gulp-underscore-jst-concat");


gulp.task('lint', function() {
   gulp.src(['./src/scripts/**/*.js'])
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});
//Less
gulp.task('less', function () {
    return gulp.src(['./src/styles/less/*.less'])
        .pipe(sourcemaps.init())
        .pipe(less({
            paths: [ path.join(__dirname, 'less', 'includes') ]
        }))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('./src/styles'));
});

//压缩css
gulp.task('minifycss', function() {
    return gulp.src([
        './src/styles/**/*.css',
        './bower_components/bootstrap/dist/css/bootstrap.min.css'])    //需要操作的文件
        .pipe(concat('render.css'))
        .pipe(gulp.dest('./dist/css'))
        .pipe(rename({suffix: '.min'}))   //rename压缩后的文件名
        .pipe(minifycss())   //执行压缩
        .pipe(gulp.dest('./dist/css'));   //输出文件夹
});

gulp.task('validate_coffee', function () {
    gulp.src('./src/scripts/**/*.coffee')
        .pipe(coffeelint())
        .pipe(coffeelint.reporter('default'));
});

gulp.task('coffee', ['validate_coffee'],function() {
    gulp.src('./src/scripts/**/*.coffee')
        .pipe(coffee({bare: true}).on('error', gutil.log))
        .pipe(concat('widget.js'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('./src/scripts/'));
});
gulp.task('scripts',['coffee'] ,function() {
    gulp.src([
        './bower_components/jquery/dist/jquery.min.js',
        './bower_components/bootstrap/dist/js/bootstrap.min.js',
        './src/scripts/**/*.js'])
        .pipe(concat('render.js'))
        .pipe(gulp.dest('./dist/javascripts'))
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify())
        .pipe(gulp.dest('./dist/javascripts'));
});

gulp.task('default', function(){
    gulp.run('less','minifycss', 'lint','scripts');
});
gulp.task('watch', function(){
    gulp.watch('./dist/javascripts/**/*.js', function(){
        gulp.run('less','minifycss', 'lint','scripts');
    });
});
